package com.example.wechat.csdnandroid.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.wechat.csdnandroid.R;
import com.example.wechat.csdnandroid.adapter.FruitOptimize2Adapter;
import com.example.wechat.csdnandroid.bean.Fruit;

import java.util.ArrayList;

public class ListView2Activity extends AppCompatActivity {

    private ListView lvAlv;

    private ArrayList<Fruit> data = new ArrayList<>();

    private static final String TAG = "ListViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        initView();
        initData();
    }


    private void initData() {
        Fruit fruit=new Fruit();
        fruit.setPicText("appale");
        fruit.setPicImageRes(R.mipmap.appale);
        data.add(fruit);
        Fruit fruit1=new Fruit();
        fruit1.setPicText("orange");
        fruit1.setPicImageRes(R.mipmap.orange);
        data.add(fruit1);
        Fruit fruit2=new Fruit();
        fruit2.setPicText("mango");
        fruit2.setPicImageRes(R.mipmap.mango);
        data.add(fruit2);
        Fruit fruit3=new Fruit();
        fruit3.setPicText("pear");
        fruit3.setPicImageRes(R.mipmap.pear);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);
        data.add(fruit3);


        FruitOptimize2Adapter arrayAdapter = new FruitOptimize2Adapter();
        arrayAdapter.setData(data, this);
        lvAlv.setAdapter(arrayAdapter);
        lvAlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG, "onItemClick: " + position);
                Log.e(TAG, "onItemClick: " + data.get(position));
            }
        });
    }

    private void initView() {
        lvAlv = (ListView) findViewById(R.id.lv_alv);
    }
}
